package com.marsproj.bankingmanagesystem.tools;

import java.util.HashMap;

public class LockBase {
    private String id;
    private static HashMap<String, LockBase> map = new HashMap<>();

    public LockBase() {
    }

    public LockBase(String id) {
        this.id = id;
    }

    public static LockBase getLockBase(String id1, String id2) {
        LockBase lockBase = null;
        // 交换大小
        if (Long.parseLong(id1) > Long.parseLong(id2)) {
            String temp = id2;
            id2 = id1;
            id1 = temp;
        }
        String id = id1 + id2;
        //
        if (!map.containsKey(id)) {
            synchronized (LockBase.class) {
                if (!map.containsKey(id)) {
                    lockBase = new LockBase(id);
                    map.put(lockBase.id, lockBase);
                    return lockBase;
                }
            }
        }
        //
        lockBase = map.get(id);
        return lockBase;
    }

    @Override
    public boolean equals(Object object) {
        return this.id.equals(((LockBase)object).id);
    }

    @Override
    public int hashCode() {
        return Integer.parseInt(id.substring(0, 9));
    }


}
